package com.ruoyi.web.controller.personnel;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.personnel.domain.AccessLogs;
import com.ruoyi.personnel.service.IAccessLogsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 访客日志记录Controller
 *
 * @author hyj
 * @date 2020-03-04
 */
@Controller
@RequestMapping("/personnel/logs")
public class AccessLogsController extends BaseController {
    private String prefix = "personnel/logs";

    @Autowired
    private IAccessLogsService accessLogsService;

    @RequiresPermissions("personnel:logs:view")
    @GetMapping()
    public String logs() {
        return prefix + "/logs";
    }

    /**
     * 查询访客日志记录列表
     */
    @RequiresPermissions("personnel:logs:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(AccessLogs accessLogs) {
        startPage();
        List<AccessLogs> list = accessLogsService.selectAccessLogsList(accessLogs);
        return getDataTable(list);
    }

    /**
     * 导出访客日志记录列表
     */
    @RequiresPermissions("personnel:logs:export")
    @Log(title = "访客日志记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AccessLogs accessLogs) {
        List<AccessLogs> list = accessLogsService.selectAccessLogsList(accessLogs);
        ExcelUtil<AccessLogs> util = new ExcelUtil<AccessLogs>(AccessLogs.class);
        return util.exportExcel(list, "logs");
    }

    /**
     * 新增访客日志记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存访客日志记录
     */
    @RequiresPermissions("personnel:logs:add")
    @Log(title = "访客日志记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AccessLogs accessLogs) {
        return toAjax(accessLogsService.insertAccessLogs(accessLogs));
    }

    /**
     * 修改访客日志记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AccessLogs accessLogs = accessLogsService.selectAccessLogsById(id);
        mmap.put("accessLogs", accessLogs);
        return prefix + "/edit";
    }

    /**
     * 修改保存访客日志记录
     */
    @RequiresPermissions("personnel:logs:edit")
    @Log(title = "访客日志记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AccessLogs accessLogs) {
        return toAjax(accessLogsService.updateAccessLogs(accessLogs));
    }

    /**
     * 删除访客日志记录
     */
    @RequiresPermissions("personnel:logs:remove")
    @Log(title = "访客日志记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(accessLogsService.deleteAccessLogsByIds(ids));
    }
}
